package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName MenuDemo
 * @Description 菜单栏
 * @Author wangss
 * @date 2020.05.18 22:39
 * @Version 1.0
 */
public class MenuDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #07ddff");

        // 菜单栏
        final MenuBar menuBar = new MenuBar();

        // 菜单
        Menu menu1 = new Menu("菜单1");
        Menu menu2 = new Menu("菜单2");
        Menu menu3 = new Menu("菜单3");
        Menu menu4 = new Menu("菜单4");

        // 菜单列表
        // 增加图片
        Image image = new Image("image/icon1.png", 30d, 30d, true, true);
        MenuItem menuItem1 = new MenuItem("item1", new ImageView(image));
        MenuItem menuItem2 = new MenuItem("item2");
        MenuItem menuItem3 = new MenuItem("item3");
        MenuItem menuItem4 = new MenuItem("item4");
        MenuItem menuItem5 = new MenuItem("item5");

        // 设置快捷键
        menuItem1.setAccelerator(KeyCombination.valueOf("ctrl+g"));

        menu1.getItems().addAll(menuItem1, menuItem2, menuItem3);
        menu2.getItems().addAll(menuItem4, menuItem5);

        menuBar.getMenus().addAll(menu1, menu2, menu3, menu4);

        anchorPane.getChildren().add(menuBar);

        Scene scene = new Scene(anchorPane);

        primaryStage.setScene(scene);
        primaryStage.setTitle("菜单");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        // 适配菜单栏的长度
        menuBar.setPrefWidth(primaryStage.getWidth());
        anchorPane.widthProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                menuBar.setPrefWidth(newValue.doubleValue());
            }
        });

        // 给子菜单增加点击事件
        menuItem1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("子菜单1");
            }
        });

        // 给菜单增加事件
        menu1.setOnShowing(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
                System.out.println("menu1.setOnShowing");
            }
        });

        menu1.setOnHidden(new EventHandler<Event>() {
            @Override
            public void handle(Event event) {
                System.out.println("menu1.setOnHidden");
            }
        });
    }
}
